package com.wsj.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wsj.dao.IMagazineClassDao;
import com.wsj.service.IMagazineClassServ;
import com.wsj.util.exception.BusinessException;
import com.wsj.util.page.PageResultSet;
import com.wsj.web.subclass.displayBean.Magazine.MagazineClassExtendBean;
import com.wsj.web.vo.homeSite.MagazineClass;

public class MagazineClassServImpl implements IMagazineClassServ {

	private IMagazineClassDao magazineClassDao; 
	
	public void add(MagazineClass magazineClass) throws BusinessException {
		this.magazineClassDao.add(magazineClass);
		magazineClass.setSequence(magazineClass.getId());
		this.magazineClassDao.update(magazineClass);
	}

	public void delete(MagazineClass magazineClass) throws BusinessException {
		this.magazineClassDao.delete(magazineClass);
	}

	/**
	 * OID 查询
	 * */
	public MagazineClass loadById(Integer id) throws BusinessException {
		MagazineClass magazineClass = null; 
		if(id!=null && !id.equals(0)){
			magazineClass = this.magazineClassDao.loadById(id);
		}
		return magazineClass;
	}

	/**
	 * 通过 MagazineName 的 ID 获取 MagazineClass 的list 集合
	 * */
	@SuppressWarnings("unchecked")
	public List loadByNameId(Integer magazineNameId) throws BusinessException {
		List<MagazineClass> list = new ArrayList<MagazineClass>();
		list = this.magazineClassDao.loadByNameId(magazineNameId);
		return list;
	}
	
	
	public Map<String,String> loadBymagazineNameIdMap(Integer magazineNameId)throws BusinessException  {
		Map<String,String> map = new HashMap<String,String>(); 
		List list =this.magazineClassDao.loadByNameId(magazineNameId);
		for(int i=0;i<list.size();i++){
			MagazineClass magazineClass= (MagazineClass)list.get(i);
			map.put(magazineClass.getId().toString(), magazineClass.getLanmuname());
		}
		return map;
	}
	

	public void update(MagazineClass magazineClass) throws BusinessException {
		this.magazineClassDao.update(magazineClass);
	}


	public IMagazineClassDao getMagazineClassDao() {
		return magazineClassDao;
	}

	public void setMagazineClassDao(IMagazineClassDao magazineClassDao) {
		this.magazineClassDao = magazineClassDao;
	}

	
	/**
	 * 条件：年份列表（时间段）、期刊名、栏目名、是否推荐、是否删除
	 * 返回： MagazineClass 集合
	 * */
	public PageResultSet loadPageList(String qkName, String lmName, Short shanchu, Short istj, String beginDate, String endDate, Integer page, Integer pageSize) throws BusinessException {
		return this.magazineClassDao.loadPageList(qkName, lmName, shanchu, istj, beginDate, endDate, page, pageSize);
	}

	
	/**
	 *  检测制定期刊名称下栏目名是否唯一
	 *  若被占用返回 true 未被占用返回 false 
	 * */
	public boolean checkName(String name, Integer NameId) throws BusinessException {
		MagazineClass magazineClass = this.magazineClassDao.loadByNameAndNameId(name, NameId);
		if(magazineClass!=null && magazineClass.getLanmuname()!=null){
			return true;
		}
		return false;
	}

	public MagazineClass loadByNameAndNameId(String name, Integer NameId) throws BusinessException 
	{
		return this.magazineClassDao.loadByNameAndNameId(name, NameId);
	}

	public List<MagazineClassExtendBean> loadMagazineClassAndNews(Integer magazineNameId) throws BusinessException
	{
		return this.getMagazineClassDao().loadMagazineClassAndNewsDao(magazineNameId);
	}

	/** 获取 指定期刊下的 前 top 的栏目 **/
	public List loadByNameId(Integer magazineNameId, Integer top) throws BusinessException {
		return this.magazineClassDao.loadTopByNameId(magazineNameId, top);
	}

}
